package org.tbjd.client.builders;

/**
 *	
 * @author Mike & Antonio
 * @version 0.1b
 * 
 * @date 30-June-08
 * 
 * This widget is the entry point of the application.
 * It allows the following functions :
 * 
 * - (OK) display list of projects in a grid representation
 * - (OK) open a project (dbl click on the row of the project)
 * -      add a new project (click on the "+" icon)
 * -      delete a project (click on the "-" icon)
 * -      find a project (enter text next to the magnifier icon)
 * -      sort columns
 * -      resize columns
 * -      filter projects by status (click on "running", "pending", "stopped", "starred", "all" icons)
 * -      set the status of a project (drop a project's row on a status icon)
 * - (OK) scrollable H&V
 * -      list of starred (favorites) projects
 *
 */

import org.tbjd.client.customWidgets.SearchBox;
import org.tbjd.client.dndControllers.projectSwitcher.GridRowPickupDragController;
import org.tbjd.client.guis.projectSwitcher.ProjectsGridGui;
import org.tbjd.client.guis.projectSwitcher.StatusGui;
import org.tbjd.client.images.menu.MenuImageBundle;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.mediators.AbstractMediator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ProjectSwitcherBuilder implements Builder, HasMediator {

	private AbstractMediator mediator = null;
	private VerticalPanel container = new VerticalPanel();
	private GridRowPickupDragController dragController = null;;
	private StatusGui statusGui = null;
	private ProjectsGridGui projectsGridGui = null;
	private SearchBox searchBoxGui = null;

	public ProjectSwitcherBuilder() {
		this.dragController = new GridRowPickupDragController(RootPanel.get());
		build();
	}

	public void build() {
		this.container.add(getTopPanel());
		this.container.add(getBottomPanel());
		this.container.setStyleName("projectSwitcher");
	}

	private Panel getTopPanel() {
		MenuImageBundle menuImageBundle = (MenuImageBundle) GWT.create(MenuImageBundle.class);
		HorizontalPanel menuPanel = new HorizontalPanel();
		menuPanel.setStyleName("projectSwitcher_menuPanel");

		// left menu
		HorizontalPanel leftMenuPanel = new HorizontalPanel();
		leftMenuPanel.setStyleName("projectSwitcher_menuPanel_left");

		PushButton buttonAdd = new PushButton(menuImageBundle.add_bw_24x24().createImage(), menuImageBundle.add_color_24x24().createImage());
		buttonAdd.setStyleName("projectSwitcher_menuPanel_button");
		// TODO add a listener on the add button
		leftMenuPanel.add(buttonAdd);
		leftMenuPanel.setCellVerticalAlignment(buttonAdd, HasVerticalAlignment.ALIGN_MIDDLE);

		PushButton buttonDelete = new PushButton(menuImageBundle.delete_bw_24x24().createImage(), menuImageBundle.delete_color_24x24().createImage());
		buttonDelete.setStyleName("projectSwitcher_menuPanel_button");
		// TODO do we need this one ?
		leftMenuPanel.add(buttonDelete);
		leftMenuPanel.setCellVerticalAlignment(buttonDelete, HasVerticalAlignment.ALIGN_MIDDLE);

		// right menu
		HorizontalPanel rightMenuPanel = new HorizontalPanel();
		rightMenuPanel.setStyleName("projectSwitcher_menuPanel_right");

		// search box gui
		this.searchBoxGui = new SearchBox();
		rightMenuPanel.add(this.searchBoxGui);

		menuPanel.add(leftMenuPanel);
		menuPanel.add(rightMenuPanel);
		menuPanel.setCellHorizontalAlignment(leftMenuPanel, HasHorizontalAlignment.ALIGN_LEFT);
		menuPanel.setCellHorizontalAlignment(rightMenuPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		menuPanel.setCellVerticalAlignment(rightMenuPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		menuPanel.setCellVerticalAlignment(leftMenuPanel, HasVerticalAlignment.ALIGN_MIDDLE);

		return menuPanel;
	}

	private Panel getBottomPanel() {
		HorizontalPanel bottomPanel = new HorizontalPanel();
		bottomPanel.setStyleName("projectSwitcher_bottomPanel");

		bottomPanel.add(getStatusPanel());
		bottomPanel.add(getProjectsGridPanel());

		return bottomPanel;
	}

	private Widget getProjectsGridPanel() {
		this.projectsGridGui = ProjectsGridGui.getInstance();
		this.projectsGridGui.setDragController(this.dragController);

		SimplePanel gridPanel = new SimplePanel();
		gridPanel.setStyleName("projectSwitcher_bottomPanel_table");
		gridPanel.add(this.projectsGridGui);

		return gridPanel;
	}

	private Widget getStatusPanel() {
		this.statusGui = StatusGui.getInstance();
		this.statusGui.setDragController(this.dragController);
		this.statusGui.setStyleName("projectSwitcher_statusPanel");

		return this.statusGui;
	}

	public Widget getBuildedWidget() {
		return this.container;
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.statusGui.setMediator(this.mediator);
		this.projectsGridGui.setMediator(this.mediator);
		this.searchBoxGui .setMediator(this.mediator);
	}

}